/*
 * @FileName: [IPUtil.java]
 * 
 * @Package com.bcinfo.upp.util
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev: 52 $
 * $LastChangedDate: 2013-11-25 11:41:03 +0800 (Mon, 25 Nov 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: wanggc $
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bcinfo.iccard.util;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
public class IPUtil
{
    // static final String REGX_IP = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
    static final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";

    /**
     * 把IP转换为LONG值 <br/>
     * <p>
     * Description: 把IP转换为LONG值，每一个IP段对应Long的一个字节 <br/>
     * <p>
     * Author: Samuel.Zhang<br/>
     * <p>
     * Date: 2013-5-30-上午10:55:38<br/>
     * <p>
     * 
     * @param ip
     * @return
     * 
     */
    public static long toLong(String ip)
    {
        if (org.apache.commons.lang3.StringUtils.isBlank(ip))
        {
            return -1;
        }

        if (!ip.matches(REGX_IP))
        {
            return -1;
        }

        String[] sipt = ip.split("\\.");
        long ipt = 0L;
        for (int i = 0; i < 4; ++i)
        {
            ipt = ipt << 8 | Integer.parseInt(sipt[i]);
        }
        return ipt;
    }

    /**
     * 判断ip是否在指定网段中
     * 
     * @author dh
     * @param iparea
     * @param ip
     * @return boolean
     */
    public static boolean ipIsInNet(String iparea, String ip)
    {
        if (iparea == null)
            throw new NullPointerException("IP段不能为空！");
        if (ip == null)
            throw new NullPointerException("IP不能为空！");
        iparea = iparea.trim();
        ip = ip.trim();

        final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
        if (!iparea.matches(REGX_IPB) || !ip.matches(REGX_IP))
            return false;
        int idx = iparea.indexOf('-');
        String[] sips = iparea.substring(0, idx).split("\\.");
        String[] sipe = iparea.substring(idx + 1).split("\\.");
        String[] sipt = ip.split("\\.");
        long ips = 0L, ipe = 0L, ipt = 0L;
        for (int i = 0; i < 4; ++i)
        {
            ips = ips << 8 | Integer.parseInt(sips[i]);
            ipe = ipe << 8 | Integer.parseInt(sipe[i]);
            ipt = ipt << 8 | Integer.parseInt(sipt[i]);
        }
        if (ips > ipe)
        {
            long t = ips;
            ips = ipe;
            ipe = t;
        }
        return ips <= ipt && ipt <= ipe;
    }
    
    public static boolean isIP(String ip){
        try
        {
            return ip.matches(REGX_IP);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args)
    {
        
        System.out.println(isIP("192.168.110.22"));
//        System.out.println(toLong("192.168.10.22"));
//        System.out.println(toLong("192.168.10.16"));
//        System.out.println(ipIsInNet("192.168.10.12-192.168.10.22", "192.168.10.16"));
    }

}
